/**
 * @param {View} shell
 * @constructor
 * @extends {BaseMediator}
 */
function MenuMediator(shell)
{
	"use strict";

	this.init(shell, '#menu');

	/** @type {MenuMediator} */
	var _this = this;

	var $first = _this.view.find('.first');
	var $second = _this.view.find('.second');
	var $third = _this.view.find('.third');

	/**
	 * Write HTML for image description and title
	 * @param {String} text
	 * @param {String} title
	 */
	function showDescription(text, title)
	{
		if (text || title)
		{
			text = (text) ? '<p>' + text + '</p>' : '';
			if (title) { text = '<h1>' + title + '</h1>' + text; }
			_this.bar.html(text);
		}
		else { _this.bar.empty(); }
	}

	/**
	 * Add CSS styling to identified list item
	 * @param {String} id Element ID
	 * @return {MenuMediator}
	 */
	this.select = function(id)
	{
		var node = document.getElementById(id);

		if (node != null)
		{
			$('li', node.parentNode).removeClass('selected');
			$('#' + id, node.parentNode).addClass('selected');
		}
		return _this;
	};

	/**
	 * @return {MenuMediator}
	 */
	this.hide = function()
	{
		shell.strip.enable();
		return _this.base.hide.call(_this);
	};

	/**
	 * @return {MenuMediator}
	 */
	this.show = function()
	{
		//_this.setup();
		shell.strip.disable();
		return _this.base.show.call(_this);
	};

	/**
	 * Show the selected set. Shadow to implement.
	 */
	this.showSet = function() { };

	/**
	 * Generate Collection Tree HTML and attach event handlers
	 * @return {MenuMediator}
	 */
	this.setup = function()
	{
		var $list = $('<ul>')
			.addClass('collections')
			.addListItems(Session.tree.list, showCollection, showDescription);

		$first.append($list);

		showCollection(Enum.flickr.defaultCollection);

		return _this;
	};

	/**
	 * Build HTML for a collection within the table of contents
	 * @param {String} id Flickr ID of the collection
	 */
	function showCollection(id)
	{
		var $list = $('<ul>');
		var parent = Session.tree.collections[id];
		var $column = (parent.atRoot) ? $second : $third;

		if (parent.collections.length > 0)
		{
			$list.addClass('collections')
				 .addListItems(parent.collections, showCollection, showDescription);
		}
		else
		{
			$list.addClass('sets')
				 .addListItems(parent.sets, _this.showSet, showDescription, true);
		}
		_this.select(id);

		$column.empty().append($list);
	}
}
MenuMediator.inherit(BaseMediator);